package com.loe.ui.view_other

import android.content.Context
import android.view.View
import androidx.annotation.Keep
import com.google.android.flexbox.*
import com.loe.ui.*
import com.loe.ui.api.IUiViewGroup
import com.loe.ui.api.ViewGroupApi
import com.loe.ui.page.UiPage
import com.quickjs.JSObject

@Keep
class UiFlexLayout : FlexboxLayout, IUiViewGroup
{
    constructor(context: Context) : super(context)

    override var uiPage: UiPage? = null

    override val jsObject by lazy { ViewGroupApi(this).jsObject }

    override fun setUI(ui: JSObject, uiPage: UiPage?): View
    {
        this.uiPage = uiPage

        // flexDirection
        ui.gotHasString("flexDirection")
        {
            flexDirection = when (it)
            {
                "column" -> FlexDirection.COLUMN
                "columnReverse" -> FlexDirection.COLUMN_REVERSE
                "rowReverse" -> FlexDirection.ROW_REVERSE
                // row
                else -> FlexDirection.ROW
            }
        }

        // divider
        ui.gotHas("divider")
        {
            setDividerV(it, ui.gotString("showDividerV"))
            setDividerH(it, ui.gotString("showDividerH"))
        }
        // dividerV
        ui.gotHas("dividerV") { setDividerV(it, ui.gotString("showDividerV")) }
        // dividerH
        ui.gotHas("dividerH") { setDividerH(it, ui.gotString("showDividerH")) }
        // maxLine
        ui.gotHasInt("maxLine") { maxLine = it }
        // alignItems
        ui.gotHasString("alignItems")
        {
            alignItems = when (it)
            {
                "baseline" -> AlignItems.BASELINE
                "stretch" -> AlignItems.STRETCH
                "center" -> AlignItems.CENTER
                "flexEnd" -> AlignItems.FLEX_END
                // flexStart
                else -> AlignItems.FLEX_START
            }
        }
        // alignContent
        ui.gotHasString("alignContent")
        {
            alignContent = when (it)
            {
                "center" -> AlignContent.CENTER
                "spaceAround" -> AlignContent.SPACE_AROUND
                "spaceBetween" -> AlignContent.SPACE_BETWEEN
                "stretch" -> AlignContent.STRETCH
                "flexEnd" -> AlignContent.FLEX_END
                // flexStart
                else -> AlignContent.FLEX_START
            }
        }
        // justifyContent
        ui.gotHasString("justifyContent")
        {
            justifyContent = when (it)
            {
                "center" -> JustifyContent.CENTER
                "spaceAround" -> JustifyContent.SPACE_AROUND
                "spaceBetween" -> JustifyContent.SPACE_BETWEEN
                "spaceEvenly" -> JustifyContent.SPACE_EVENLY
                "flexEnd" -> JustifyContent.FLEX_END
                // flexStart
                else -> JustifyContent.FLEX_START
            }
        }
        // flexWrap
        flexWrap = when (ui.gotString("flexWrap"))
        {
            "wrapReverse" -> FlexWrap.WRAP_REVERSE
            "nowrap" -> FlexWrap.NOWRAP
            // wrap
            else -> FlexWrap.WRAP
        }

        // click
        ui.gotHasBoolean("isClick") { setClick(it) }

        // 递归子元素
        ui.gotHasArray("childs")
        { childs ->
            childs.forEach<JSObject>
            {
                uiPage?.createUI(it, this)
            }
        }
        return this
    }

    fun initChildParams(ui: JSObject, params: LayoutParams)
    {
        // alignSelf
        ui.gotHasString("alignSelf")
        {
            params.alignSelf = when (it)
            {
                "baseline" -> AlignItems.BASELINE
                "stretch" -> AlignItems.STRETCH
                "center" -> AlignItems.CENTER
                "flexEnd" -> AlignItems.FLEX_END
                // flexStart
                else -> AlignItems.FLEX_START
            }
        }
        // flexGrow
        ui.gotHasDouble("flexGrow") { params.flexGrow = it.toFloat() }
        // flexShrink
        ui.gotHasDouble("flexShrink") { params.flexShrink = it.toFloat() }
        // flexBasisPercent
        ui.gotHasDouble("flexBasisPercent") { params.flexBasisPercent = it.toFloat() }
        // wrapBefore
        ui.gotHasBoolean("wrapBefore") { params.isWrapBefore = it }

        // maxHeight
        ui.gotHasString("maxHeight") { params.maxHeight = it.px }
        // maxWidth
        ui.gotHasString("maxWidth") { params.maxWidth = it.px }
        // minHeight
        ui.gotHasString("minHeight") { params.minHeight = it.px }
        // minWidth
        ui.gotHasString("minWidth") { params.minWidth = it.px }

        // order
        ui.gotHasInt("order") { params.order = it }
    }

    override var onSelect: (() -> Unit)? = null

    fun setDividerV(divider: Any, showDividerV: String)
    {
        if (divider is JSObject)
        {
            dividerDrawableHorizontal = divider.shape
        } else
        {
            divider.toString().preLoadCallback(context) { drawable -> dividerDrawableHorizontal = drawable }
        }
        // showDividerV
        showDividerHorizontal = when (showDividerV)
        {
            "end" -> SHOW_DIVIDER_END
            "none" -> SHOW_DIVIDER_NONE
            "beginning" -> SHOW_DIVIDER_BEGINNING
            // middle
            else -> SHOW_DIVIDER_MIDDLE
        }
    }

    fun setDividerH(divider: Any, showDividerH: String)
    {
        if (divider is JSObject)
        {
            dividerDrawableVertical = divider.shape
        } else
        {
            divider.toString().preLoadCallback(context) { drawable -> dividerDrawableVertical = drawable }
        }
        // showDividerH
        showDividerVertical = when (showDividerH)
        {
            "end" -> SHOW_DIVIDER_END
            "none" -> SHOW_DIVIDER_NONE
            "beginning" -> SHOW_DIVIDER_BEGINNING
            // middle
            else -> SHOW_DIVIDER_MIDDLE
        }
    }

    /********************************************** 点击 ****************************************/

    fun setClick(isClick: Boolean)
    {
        isAlpha = isClick
        isClickable = isClick
    }

    private var isAlpha = false

    override fun dispatchSetPressed(pressed: Boolean)
    {
        if (isAlpha) alpha = if (pressed || !isEnabled) 0.75f else 1f
        super.dispatchSetPressed(pressed)
    }

    override fun setEnabled(enabled: Boolean)
    {
        if (isAlpha) alpha = if (enabled) 1f else 0.75f
        super.setEnabled(enabled)
    }
}